const {RequestMethod} = require("../core/config/RequestConfig");
const userController = require("../controller/UserController")
const Logger = require("../core/utils/Logger");

const routes = [
    {
        prefix: "/v1",
        path: "",
        sub: [
            {
                path: "/sys_user",
                handle: undefined,
                label: "用户相关接口",
                method: RequestMethod.GET,
                sub: [
                    {
                        prefix: "/list",
                        path: "",
                        handle: userController.querySystemUsers(),
                        label: "分页查询系统用户列表",
                        method: RequestMethod.GET,
                        sub: [],
                    },
                    {
                        prefix: "/useLogin",
                        path: "",
                        handle: userController.login(),
                        label: "系统用户登录接口",
                        method: RequestMethod.POST,
                        sub: [],
                    },
                    {
                        prefix: "",
                        path: "/register",
                        handle: userController.register(),
                        label: "系统用户登录接口",
                        method: RequestMethod.POST,
                        sub: [],
                    }
                ]
            }
        ]
    },
    {
        prefix: "/menus",
        path: "",
        handle: undefined,
        method: RequestMethod.GET,
        sub: [
            {
                path: "/mes",
                handle: undefined,
                method: RequestMethod.GET,
            }
        ]
    }
]


/**
 * 构建路由
 * @param router   express app 对象
 * @param routes   路由 map / json 信息
 * @param parentPath  父路径
 */
function buildRoutes(router, routes = [], parentPath = "") {
    
    routes.forEach(route => {
        // 计算当前路由的完整路径
        const fullPath = parentPath + ((route.prefix || "") + (route.path || "") || "");

        // 如果有具体的路径和处理函数，添加路由
        if (route.handle && route.method) {
            Logger.log(`【接口名称】-【 ${route.label}】 - 【请求路径】 - 【${fullPath}】 `)
            router[route.method.toLowerCase()](fullPath, route.handle);
        }

        // 如果有子路由，递归处理子路由
        if (route.sub && route.sub.length > 0) {
            buildRoutes(router, route.sub, fullPath);
        }
    });
}


module.exports = {
    buildRoutes,
    routes
}

